<template>
    <div class="model-tags" v-show="tags.length > 1">
        <div class="model-tags-button" @click="openDialog()">
            <button>{{getTagName(value)}}</button>
        </div>
        <UxDialog :visible="visible" title="" @close="visible = false">
            <div class="tags-dialog" v-loading="loading">
                <svg-icon name="design-tag" />
                <h2>Select a mockup to preview your design</h2>
                <h3>
                    <span v-for="item in tags" :key="item" @click="setActive(item)" :class="{active:item==tag}" v-text="getTagName(item)"></span>
                </h3>

                <p v-show="tag=='hanging'">If your design is primarily centered, hanging mockups can create a more impactful effect.</p>
                <p v-show="tag=='flat lay'">Flat lay mockups are a good way to effectively showcase your design.</p>
                <p v-show="tag=='model'">Displaying your design on a model significantly enhances the overall presentation quality.</p>

                <div class="tags-dialog-model">
                    <div class="tags-dialog-model-visible" v-for="model in modelTags" :key="model.id">
                        <DesignerModelPanelRender :model="model" :pieces="pieces" :color="activeColor" :activePiece="activePiece" />
                    </div>
                </div>

                <a @click="global = true">Apply to all products</a>
                <button @click="setApply()" type="primary">Confirm</button>
            </div>
        </UxDialog>

        <UxDialog :visible="global" title="" @close="global = false">

            <div class="tags-global" v-loading="loading">
                <figure>
                    <img src="@/assets/images/designer/designer-models-tag.png" alt="">
                </figure>

                <p>Once applied, the selected mockup image type will automatically display as the preview in the Design Creator, if supported by the product. Otherwise, a default image type will be used for the preview.</p>
                <fieldset>
                    <button type="primary" @click="setGlobalApply()">Confirm</button>
                    <button @click="global = false">Cancel</button>
                </fieldset>
            </div>

        </UxDialog>
    </div>
</template>

<script>
import { mapGetters, mapState } from "vuex";
export default {
    props: {
        value: { type: String, default: '' },
        spu: { type: String, default: '' },
    },
    data () {
        return {
            visible: false,
            global: false,
            loading: false,
            size: { width: 1200, height: 1200 },
            modelTags: [],
            tag: '',
        }
    },
    computed: {
        ...mapGetters("designer", {
            models: "getModels",
            pieces: "getPieces",
            colors: "getColors",
            activePiece: "getActivePiece",
            activeModel: "getActiveModel",
            activeColor: "getActiveColor",
        }),
        ...mapGetters({
            logined: "getIsLogin",
        }),

        tags () {
            const list = [
                'hanging',
                'flat lay',
                'model',
            ]
            return list.filter(i => this.models.find(model => model.model_tag == i))
        },
    },

    methods: {

        getTagName (tag) {
            const model = this.models.find(model => model.model_tag == tag) || {}
            return model.model_name || 'Hanging'

            // const list = new Map([
            //     ['hanging', 'Hanging'],
            //     ['flat lay', 'Flat lay'],
            //     ['model', 'Model'],
            // ])
            // return list.get(tag) || 'Hanging'
        },

        // 打开弹窗
        openDialog () {
            this.tag = ''
            this.visible = true
            this.global = false;
            this.modelTags = []
            // 猥琐的更新办法
            this.$forceUpdate()
            this.$nextTick().then(() => setTimeout(() => this.setActive(this.value), 1))
        },

        // 选中标签
        setActive (name) {
            this.tag = name;
            this.modelTags = this.models.filter(model => model.model_tag == this.tag)
        },

        // 应用当前
        setApply () {

            if (this.logined) {
                this.loading = true;
                this.$http('User.setSpuModelTag', { model_tag: this.tag, spu: this.spu }).then(res => {
                    if (res.code === 0) {
                        this.$emit('input', this.tag)
                        this.loading = false;
                        this.visible = false;
                    } else {
                        this.$uxMessage.error(res.msg)
                        this.loading = false;
                    }
                })

            } else {
                this.$emit('input', this.tag)
                this.visible = false;
            }

        },

        // 全局应用
        setGlobalApply () {

            const doRequest = () => {
                this.loading = true;
                this.$http('User.setUserModelTag', { model_tag: this.tag }).then(res => {
                    if (res.code === 0) {
                        this.$emit('input', this.tag)
                        this.loading = false;
                        this.visible = false;
                        this.global = false;
                    } else {
                        this.$uxMessage.error(res.msg)
                        this.loading = false;
                    }
                })
            }

            if (this.logined) {
                doRequest()
            } else {
                this.$emit("global:userSignup", (e) => { });
            }

        }
    }
};
</script>

<style lang="scss" scoped>
.model-tags-button {
    & > button {
        display: flex;
        align-items: center;
        justify-content: center;
        min-width: 96px;
        height: 28px;
        border-radius: 6px;
        border: 1px solid #e0e0e0;
        color: #555;
        font-size: 14px;
        font-family: Roboto-Medium, Roboto;
        font-weight: 500;

        svg {
            width: 16px;
            height: 16px;
            margin-left: 4px;
        }

        &:hover {
            color: #ea4c89;
        }
    }
}
.tags-dialog {
    width: 728px;
    box-sizing: border-box;
    padding: 36px 24px 40px;
    display: flex;
    flex-direction: column;
    align-items: center;

    & > svg {
        width: 28px;
        height: 28px;
        min-height: 28px;
    }

    & > h2 {
        height: 18px;
        font-size: 16px;
        font-family: Roboto-Medium, Roboto;
        font-weight: 500;
        color: #000000;
        line-height: 18px;
        margin-top: 16px;
    }

    & > h3 {
        margin-top: 20px;
        height: 32px;
        box-sizing: border-box;
        padding: 2px;
        display: flex;
        background: #f0f0f0;
        border-radius: 6px;
        span {
            width: 116px;
            height: 28px;
            line-height: 28px;
            border-radius: 5px;
            font-size: 12px;
            font-family: Roboto-Medium, Roboto;
            font-weight: 500;
            color: #555555;
            text-align: center;
            cursor: pointer;

            & + span {
                margin-left: 2px;
            }

            &:hover {
                background-color: rgba(255, 255, 255, 0.6);
                color: #000;
            }

            &.active {
                background-color: #fff;
                color: #000;
            }
        }
    }

    & > p {
        margin-top: 20px;
        font-size: 12px;
        font-family: Roboto-Regular, Roboto;
        font-weight: 400;
        color: #555555;
        line-height: 18px;
    }

    .tags-dialog-model {
        display: flex;
        justify-content: center;
        border: 1px solid #e6e6e6;
        border-radius: 8px;
        margin-top: 20px;
        height: 340px;
        width: 680px;
        min-height: 340px;
        position: relative;
        overflow: hidden;
        .tags-dialog-model-visible {
            width: 340px;
            height: 340px;
            position: relative;

            &::v-deep {
                canvas {
                    position: absolute;
                    width: 100%;
                    height: 100%;
                }
            }

            // display: flex;
            // opacity: 0;
            // position: absolute;
            // left: 0;
            // top: 0;
            // width: 100%;
            // height: 100%;
            // figure {
            //     display: flex;
            //     position: relative;
            //     width: 340px;
            //     height: 340px;

            // }
            // &.active {
            //     opacity: 1;
            // }
        }

        .template-preview {
            width: 340px;
            height: 340px;
        }
    }

    & > a {
        margin-top: 14px;
        margin-left: auto;
        text-align: right;
        font-size: 12px;
        font-family: Roboto-Medium, Roboto;
        font-weight: 500;
        color: #3199bd;
        line-height: 18px;
        line-height: 18px;
        text-decoration: underline;
        cursor: pointer;

        &:hover {
            color: #287e9c;
            text-decoration: none;
        }
    }

    & > button {
        width: 140px;
        height: 36px;
        min-height: 36px;
        border-radius: 6px;
        font-size: 14px;
        font-family: Roboto-Medium, Roboto;
        font-weight: 500;
    }
}

.tags-global {
    width: 320px;
    height: 414px;
    padding: 40px 14px 14px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    align-items: center;
    figure {
        width: 80px;
        height: 80px;
        img {
            width: 100%;
            height: 100%;
        }
    }

    p {
        flex: 1;
        margin-top: 30px;
        font-size: 14px;
        font-family: Roboto-Regular, Roboto;
        font-weight: 400;
        color: #000000;
        line-height: 22px;
        text-align: center;
    }
    fieldset {
        width: 100%;
        display: flex;
        flex-direction: column;

        button {
            height: 38px;
            & + button {
                margin-top: 8px;
            }
        }
    }
}
</style>
